package com.shimepu.fragmentdemo.login;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.shimepu.fragmentdemo.findcode.FindCode;
import com.shimepu.fragmentdemo.main_page.MainActivity;
import util.OperateData;
import com.shimepu.fragmentdemo.R;
import com.shimepu.fragmentdemo.register.RegisterActivity;

import java.net.MalformedURLException;
import java.net.URL;


public class LoginActivity extends AppCompatActivity {
    private EditText phone;
    private EditText password;
    private Button login;
    private Button registernow;
    private Button forgetcode;
    private Button yinsizhengce;
    public static final String TAG = "LoginActivity";
    private static final String URLLOGIN = "xxx/login/json/data";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.logindemo);

        //绑定控件
        init();

         //给登录按钮设置监控
        login.setOnClickListener(new OnClickListener() {
            @Override

            //登录按键的响应
            public void onClick(View v) {

                String[] data = null;
                String inputPhone = phone.getText().toString();
                String inputPassword = password.getText().toString();

                if (TextUtils.isEmpty(inputPhone)) {
                    Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(inputPassword)) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else {
                    data = new String[]{inputPhone, inputPassword };
                    @SuppressLint("HandlerLeak") Handler handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            switch (msg.what) {
                                case 0:
                                    Toast.makeText(LoginActivity.this, "服务器连接失败", Toast.LENGTH_SHORT).show();
                                    break;
                                case 1:
                                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                    LoginActivity.this.finish();
                                    break;
                                case 2:
                                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                                    break;
                                case 3:
                                    Log.e("input error", "url为空");
                                    break;
                                case 4:
                                    Toast.makeText(LoginActivity.this, "连接超时", Toast.LENGTH_SHORT).show();
                                    break;
                                default:
                            }
                        }
                    };
                    OperateData operateData = new OperateData();
                    String jsonString = operateData.stringTojson(data);
                    URL url = null;
                    try {
                        url = new URL(URLLOGIN);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                    operateData.sendData(jsonString, handler, url);

                }

            }
        });

        /**
         * 忘记密码按钮监控，跳转到验证页面
         */
             forgetcode.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     Intent intent =new Intent(LoginActivity.this, FindCode.class);
                     startActivity(intent);
                 }
             });



        /**
         * 跳转到注册页面
         */
        registernow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);

            }
        });



        /**
         * 隐私政策按钮，跳转到隐私政策页面
         */
        yinsizhengce.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(LoginActivity.this, com.shimepu.fragmentdemo.yinsizhengce.yinsizhengce.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 初始化xml布局中的id 属性
     */
    private void init() {
        login = findViewById(R.id.login);
        registernow = findViewById(R.id.registernow);
        phone = findViewById(R.id.phone);
        password = findViewById(R.id.password);
        forgetcode=findViewById(R.id.forgetcode);
        yinsizhengce=findViewById(R.id.yinsizhengce);
    }

}
